package com.lanjiu.lib.imclient.dto.chat;

import android.text.TextUtils;

import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.protobuf.ByteString;
import com.lanjiu.lib.utils.log.Logger;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.lib.imclient.dto.base.Dto;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @description: 视频
 * @author: Panwei
 * @date: 2019/11/14 21:11
 */
public class VideoDto extends Dto {

    // 原视频下载URL
    private String url;

    // 缩略图
    private String thumbnail;

    // 当前坐标
    private long pos;

    // 当前文件内容大小
    private long length;

    public VideoDto() {
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    public long getPos() {
        return pos;
    }

    public void setPos(long pos) {
        this.pos = pos;
    }

    public long getLength() {
        return length;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public VideoDto(BusinessProtocolEntities.Video video, String thumbPath) {
        this.url = video.getUrl();
        this.thumbnail = thumbPath;
        this.pos = video.getPos();
        this.length = video.getLength();
    }

    @Override
    public BusinessProtocolEntities.Video toProtobuf() {
        BusinessProtocolEntities.Video.Builder builder = BusinessProtocolEntities.Video.newBuilder();
        if (null != url) builder.setUrl(url);
        if (!TextUtils.isEmpty(thumbnail)) {
            try {
                InputStream is = new FileInputStream(thumbnail);
                builder.setThumbnail(ByteString.readFrom(is));
                is.close();
            } catch (IOException e) {
                Logger.print("Exception", e);
            }
        }
        return builder.setPos(pos)
                .setLength(length)
                .build();
    }

    @Override
    public String toJson() {
        return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(this);
    }
}
